//////////////////////////////////////////////////////////////////////////

#ifndef _INPUTIMPL_H_
#include "inputimpl.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include <SDL/SDL.h>

//////////////////////////////////////////////////////////////////////////


InputImpl::InputImpl()
{
}


//////////////////////////////////////////////////////////////////////////


InputImpl::~InputImpl()
{
}


//////////////////////////////////////////////////////////////////////////


void InputImpl::ProcessInput()
{
    // SDL event
    SDL_Event event;

    // Process all events
    while( SDL_PollEvent(&event) )
    {
        ProcessEvent(event);
    }
}


//////////////////////////////////////////////////////////////////////////


void InputImpl::ProcessEvent( SDL_Event _event )
{
    bool notifyKeyState = false;
    bool notifyMousePosition = false;


    // Convert from SDL to internal type
    switch (_event.type)
    {
    case SDL_QUIT:
        {
            m_Key.m_Code = Input::EXIT;
            notifyKeyState = true;
            break;
        }

    case SDL_KEYDOWN:
        {
            m_Key.m_State = Input::PRESSED;
            switch (_event.key.keysym.sym)
            {
            case SDLK_w:
                m_Key.m_Code = Input::W;
                notifyKeyState = true;
                break;
            case SDLK_s:
                m_Key.m_Code = Input::S;
                notifyKeyState = true;
                break;
            case SDLK_a:
                m_Key.m_Code = Input::A;
                notifyKeyState = true;
                break;
            case SDLK_d:
                m_Key.m_Code = Input::D;
                notifyKeyState = true;
                break;
            default:
                break;
            }

            break;
        }

    case SDL_KEYUP:
        {
            m_Key.m_State = Input::RELEASED;
            switch (_event.key.keysym.sym)
            {
            case SDLK_w:
                m_Key.m_Code = Input::W;
                notifyKeyState = true;
                break;
            case SDLK_s:
                m_Key.m_Code = Input::S;
                notifyKeyState = true;
                break;
            case SDLK_a:
                m_Key.m_Code = Input::A;
                notifyKeyState = true;
                break;
            case SDLK_d:
                m_Key.m_Code = Input::D;
                notifyKeyState = true;
                break;
            default:
                break;
            }

            break;
        }

    case SDL_MOUSEBUTTONDOWN:
        {
            m_Key.m_State = Input::PRESSED;
            switch (_event.button.button)
            {
            case SDL_BUTTON_LEFT:
                m_Key.m_Code = Input::MOUSE1;
                notifyKeyState = true;
                break;
            default:
                break;
            }
            break;
        }

    case SDL_MOUSEBUTTONUP:
        {
            m_Key.m_State = Input::RELEASED;
            switch (_event.button.button)
            {
            case SDL_BUTTON_LEFT:
                m_Key.m_Code = Input::MOUSE1;
                notifyKeyState = true;
                break;
            default:
                break;
            }
            break;
        }

    case SDL_MOUSEMOTION:
        {
            m_MousePosition.x = _event.motion.x;
            m_MousePosition.y = _event.motion.y;
            notifyMousePosition = true;
            break;
        }

    default:
        break;
    }

    if (notifyKeyState)
        NotifyObservers(Observer::KEY_INPUT_CHANGE);

    if (notifyMousePosition)
        NotifyObservers(Observer::MOUSE_POSITION_CHANGE);
}


//////////////////////////////////////////////////////////////////////////


Input::Key InputImpl::GetKey() const
{
    return m_Key;
}


//////////////////////////////////////////////////////////////////////////


Input::MousePosition InputImpl::GetMousePosition() const
{
    return m_MousePosition;
}


//////////////////////////////////////////////////////////////////////////
